package dashboard.repository;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import dashboard.domain.Member;

@Repository("MemberDao")
public class MemberDaoImpl implements MemberDao {

	private HibernateTemplate hibernateTemplate;
	
	protected final Log logger = LogFactory.getLog(getClass());
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory){
		this.hibernateTemplate = new HibernateTemplate(sessionFactory);
	}

	@Override
	public int insert(Member member) throws Exception {
		logger.info("insert new memeber into DB - id: " + member.getId());

		hibernateTemplate.persist(member);
		hibernateTemplate.flush();
				
		return member.getId();
	}

	@Override
	public List<Member> listAllMembers() throws Exception {
		return hibernateTemplate.find("from Member");
	}
}
